<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$catelevel=pdo_get('zh_gjw_catelevel',array('uniacid'=>$_W['uniacid']));
$cates = pdo_fetchall("SELECT * FROM " . tablename('zh_gjw_shotcate')."WHERE uniacid=".$_W['uniacid']);
$faths=$cates;
function gethtmlTree($data, $pId,$num=0,$_W,$catelevel)
{
    $html = '';
    $num++;
    foreach($data as $k => $v)
    {
       if($v['p_id'] == $pId)
       {         //父亲找到儿子
        $html .= '<ol>';
        $html .=    '<li>';
        $html .=        '<div class="form-group tcate">';
        $html .=            '<div><div>';
        if ($num < $catelevel) {
            $html .='<span class="minus">-</span><span class="plus">+</span>';
        }else{
            $html .='<span style="display:none;" class="minus minuslast">-</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="plus">+</span>';
        }
        $html .=                '</div><img src="'.$_W['attachurl'].$v['img'].'">'.$v['cate'].'</h6></div>';
        $html .=            '<div>';
        if ($v['showhide'] == 0) {
            $html .=                '<a href="'.$_W['siteroot'].'/web/index'.'.php?c=site&i='.$_W['uniacid'].'&a=entry&do=shotcate&m=zh_gjw&op=hotx&id='.$v['id'].'" class="storespan btn btn-xs">';
            $html .=                    '<input style="border:none;background:none;" type="button" value="禁用">';
            $html .=                '</a>';
        }
        if ($v['showhide'] == 1) {
            $html .=                '<a href="'.$_W['siteroot'].'/web/index'.'.php?c=site&i='.$_W['uniacid'].'&a=entry&do=shotcate&m=zh_gjw&op=hoty&id='.$v['id'].'" style="background:#44ABF7;" class="storespan btn btn-xs">';
            $html .=                    '<input style="border:none;background:#44ABF7;color:#fff;" type="button" value="启用">';
            $html .=                '</a>';
        }
        $html .=                '<a';
        if ($num < $catelevel) {
            
        }else{
            $html .=' style="display:none"';
        }
        $html .=                    ' href="'.$_W['siteroot'].'/web/index'.'.php?c=site&i='.$_W['uniacid'].'&a=entry&do=addshotcate&m=zh_gjw&ttid='.$v['id'].'" class="storespan btn btn-xs">';
        $html .=                    '<span class="fa fa-plus"></span>';
        $html .=                    '<span class="bianji">添加<span class="arrowdown"></span></span>';
        $html .=                '</a>';
        $html .=                '<a href="'.$_W['siteroot'].'/web/index'.'.php?c=site&i='.$_W['uniacid'].'&a=entry&do=addshotcate&m=zh_gjw&id='.$v['id'].'&tpid='.$v['p_id'].'" class="storespan btn btn-xs">';
        $html .=                    '<span class="fa fa-pencil"></span>';
        $html .=                    '<span class="bianji">编辑<span class="arrowdown"></span></span>';
        $html .=                '</a>';
        $html .=                '<a href="'.$_W['siteroot'].'/web/index'.'.php?c=site&i='.$_W['uniacid'].'&a=entry&op=delete&do=shotcate&m=zh_gjw&id='.$v['id'].'" class="storespan btn btn-xs">';
        $html .=                    '<span class="fa fa-trash-o"></span>';
        $html .=                    '<span class="bianji">删除<span class="arrowdown"></span></span>';
        $html .=                '</a>';
        $html .=            '</div>';
        $html .=        '</div>';
        $html .= gethtmlTree($data, $v['id'],$num,$_W,$catelevel);
        $html .=    '</li>';
        $html .= '</ol>';
       }
    }
    return $html;
}
$cates=gethtmlTree($cates, 0,$num,$_W,$catelevel['shotcate']);

$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op=='delete'){
    function delall($faths,$id){
      foreach ($faths as $key => $value) {
        if ($value['id']==$id) {
          pdo_delete('zh_gjw_shotcate',array('id'=>$value['id']));
          $s_id=pdo_getall('zh_gjw_shotcate',array('p_id'=>$value['id']));
          for ($i=0; $i <count($s_id) ; $i++) { 
            delall($faths,$s_id[$i]['id']);
          }
        }
      }
    }
    delall($faths,$_GPC['id']);
    message('删除成功',$this->createWebUrl('shotcate',array()),'success');
}
if($_GPC['op']=='hoty'){
    $res=pdo_update('zh_gjw_shotcate',array('showhide'=>0),array('id'=>$_GPC['id']));
    if($res){
     message('', 'referer', 'success');
    }else{
          message('失败！','','error');
    }
}
if($_GPC['op']=='hotx'){
    $res=pdo_update('zh_gjw_shotcate',array('showhide'=>1),array('id'=>$_GPC['id']));
    if($res){
     message('', 'referer', 'success');
    }else{
          message('失败！','','error');
    }
}
include $this->template('web/cuscate');